package com.mr.qa.dao.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.Query;

import com.mr.qa.bo.ShopArticle;

public class ShopArticleDAO extends BaseHibernateDAO {
	
private static final Log log = LogFactory.getLog(ShopArticleDAO.class);
	
	public void save(ShopArticle transientInstance) 
    {
        try 
        {
            getSession().save(transientInstance);
        }
        catch(RuntimeException re) 
        {
            throw re;
        }
    }
	
	public void modify(ShopArticle transientInstance) 
    {
        try 
        {
            getSession().update(transientInstance);
        }
        catch(RuntimeException re) 
        {
            throw re;
        }
    }

    
    public void delete(ShopArticle persistentInstance) 
    {
        log.debug("deleting ShopArticle instance");
        try 
        {
            getSession().delete(persistentInstance);
            log.debug("delete successful");
        }
        catch (RuntimeException re) 
        {
            log.error("delete failed", re);
            throw re;
        }
    }
    
    public ShopArticle findById(Integer id) {
        try {
        	ShopArticle instance = (ShopArticle) getSession()
                    .load("com.mr.qa.bo.ShopArticle", id);
            return instance;
        } catch (RuntimeException re) {
            throw re;
        }
    }
    
    public ShopArticle findByName(String name) {
        try {
        	String sql = "from ShopArticle as model where model.name = ?";
        	HashMap map = new HashMap();
    		map.put(0, name);
    		List result = this.getQueryList(sql, map);
    		if(result == null || result.isEmpty()){
    			return null;
    		}
    		return (ShopArticle)result.get(0);
        } catch (RuntimeException re) {
            throw re;
        }
    }
    
    public ShopArticle findByNameExceptId(String name, Integer id) {
        try {
        	String sql = "from ShopArticle as model where model.name = ? and model.id != ?";
        	HashMap map = new HashMap();
    		map.put(0, name);
    		map.put(1, id);
    		List result = this.getQueryList(sql, map);
    		if(result == null || result.isEmpty()){
    			return null;
    		}
    		return (ShopArticle)result.get(0);
        } catch (RuntimeException re) {
            throw re;
        }
    }
	
	public Integer countArticlesByCategory(Integer categoryId){
		String sql = "select count(*) from ShopArticle as model where model.category.id = ?";
		HashMap map = new HashMap();
		map.put(0, categoryId);
		return this.countQuery(sql, map);
	}
	
	public List getArticles(String whereClause, String orderBy){
		StringBuffer sql = new StringBuffer("from ShopArticle");
		if(whereClause != null){
			sql.append(" " + whereClause);
		}
		if(orderBy != null){
			sql.append(" " + orderBy);
		}
		return this.getQueryList(sql.toString(), null);
	}
	
	public Integer getArticlesCount(String whereClause, String orderBy){
		StringBuffer sql = new StringBuffer("select count(*) from ShopArticle");
		if(whereClause != null){
			sql.append(" " + whereClause);
		}
		if(orderBy != null){
			sql.append(" " + orderBy);
		}
		return this.countQuery(sql.toString(), null);
	}
	
	public List getAllArticlesExchangeList(Integer categoryId){
		String sql = null;
		if(categoryId != null){
			sql = "select article_id from (select article_id from mr_iask_shop_article where category_id=" + categoryId + " and quantity <> 0 order by money) as temp1" +
				" union all" +
				" select article_id from (select article_id from mr_iask_shop_article where category_id=" + categoryId + " and quantity = 0 order by money) as temp2";
		}else{
			sql = "select article_id from (select article_id from mr_iask_shop_article where quantity <> 0 order by money) as temp1" +
			" union all" +
			" select article_id from (select article_id from mr_iask_shop_article where quantity = 0 order by money) as temp2";
		}
		Query query = getSession().createSQLQuery(sql);
		if(this.getPageNo().intValue() > 0 && this.getRecordCount().intValue() > 0)
        {
			query.setFirstResult((getPageNo().intValue() - 1) * getRecordCount().intValue());
			query.setMaxResults(getRecordCount().intValue());
        }
		List list = query.list();
		List result = new ArrayList();
		for (Iterator iterator = list.iterator(); iterator.hasNext();) {
			Object object = (Object) iterator.next();
			ShopArticle article = this.findById((Integer)object);
			result.add(article);
		}
		return result;
	}
}
